﻿using System.Web.Mvc;
using System.Web.Routing;
using RouteMagic;
using TSharp.Core.Mvc;
using TSharp.Core.Mvc.Web.MagicRouteDemo;

//[assembly: RegRoute(typeof(MagicRouteRegisterDemo), Order = int.MaxValue)]

namespace TSharp.Core.Mvc.Web.MagicRouteDemo
{
    /// <summary>
    /// </summary>
    /// <author>
    ///     tangjingbo
    /// </author>
    /// <remarks>
    ///     tangjingbo at 2009-8-5 10:32
    /// </remarks>
    public class MagicRouteRegisterDemo : IRouteRegister
    {

        public static void RegisterRoutes(RouteCollection routes)
        {
            var namespaces = new[] { "TSharp.Core.SystemOption.Controllers" };
            // Redirect From Old Route to New route
            Route targetRoute = routes.Map("target",
                                           "yo/{id}/{action}",
                                           new { action = "index", controller = "Home", area = "System" }, null,
                                           namespaces);

            routes.Redirect(r =>
                            r.MapRoute("legacy", "foo/{id}/baz/{action}", new { controller = "Home", area = "System" },
                                       null, namespaces))
                  .To(targetRoute, new { id = "123", action = "index", area = "System" }
                );

            routes.Redirect(r =>
                            r.MapRoute("legacy2", "foo/baz", namespaces))
                  .To(targetRoute, new { id = "123", action = "index", area = "System" });
            routes.MapRoute(
                "Default2", // Route name
                "{controller}/{action}/{id}.html", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
                , new { id = "\\d+" }
                );

            //// Map Delegate
            routes.MapDelegate("map-delegate", "this-is-a-test",
                               rc => rc.HttpContext.Response.Write("Yeah, it's a test"));

            //// Map HTTP Handlers
            routes.MapHttpHandler<HelloWorldHttpHandler>("hello-world", "handlers/helloworld");
            routes.MapHttpHandler("hello-world2", "handlers/helloworld2", new HelloWorldHttpHandler());

            var someRoutes = new RouteCollection();
            someRoutes.MapHttpHandler<HelloWorldHttpHandler>("hello-world3", "handlers/helloworld3");
            someRoutes.MapHttpHandler("hello-world4", "handlers/helloworld4", new HelloWorldHttpHandler());
            var groupRoute = new GroupRoute("~/section", someRoutes);
            routes.Add("group", groupRoute);

            var mvcRoutes = new RouteCollection();
            mvcRoutes.Map("foo1", "foo/{controller}", new { action = "index" }, null, namespaces);
            mvcRoutes.Map("foo2", "foo2/{controller}", new { action = "index" });
            routes.Add("group2", new GroupRoute("~/group2sec", mvcRoutes));
        }

        void IRouteRegister.RegisterRoutes(RouteCollection routes)
        {
            RegisterRoutes(routes);
        }
    }
}